package com.jzr.board.config.mutildb;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.jzr.common.util.ArrayUtil;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = {"com.jzr.board.db1.mapper"},sqlSessionFactoryRef = "db1SqlSessionFactory")
public class Db1Config {
    @Bean(name = "db1PaginationInterceptor")
    public PaginationInterceptor db1PaginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //paginationInterceptor.setLocalPage(true);
        return paginationInterceptor;
    }

    @Bean(name = "db1")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.hikari.db1")
    public DataSource db1() {
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
        //return new HikariDataSource();
        //return DruidDataSourceBuilder.create().build();
    }


    @Bean(name = "db1SqlSessionFactory")
    @Primary
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1PaginationInterceptor") PaginationInterceptor db1PaginationInterceptor,
                                                  @Qualifier("db1") DataSource db1 ) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(db1);

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);
        //PerformanceInterceptor(),OptimisticLockerInterceptor()
        //添加分页功能
        //PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        sqlSessionFactory.setPlugins(new Interceptor[]{
                db1PaginationInterceptor
        });
//        sqlSessionFactory.setGlobalConfig(globalConfiguration());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources1 =resolver.getResources("classpath*:mapper/db1/*.xml");
        sqlSessionFactory.setMapperLocations(ArrayUtil.mergeArrays(resources1));
        return sqlSessionFactory.getObject();
    }

}